#include <sys/msg.h>

#define MSG_PATHNAME "msg_queue.c"
#define MSG_PROJID 10

#define MSG_TYPE 5

struct PacId
{
	unsigned int destIp;
	unsigned int sourIp;
	unsigned short destPort;
	unsigned short sourPort;
};


struct JudgeOrder
{
	struct PacId pacid;
	int judge;
};


//定义消息结构
typedef struct MsgBuf
{
	long msgtype;
	struct PacId pacid;
}My_Msg;

//a new message format
typedef struct 
{
	long msgtype;
	struct JudgeOrder judge_order;
}Judge_Msg;

//to construct a judge message
Judge_Msg * getJudgeMsg(struct PacId pacid,int judge)
{
	int msg_len=sizeof(Judge_Msg)-4;
	Judge_Msg * jm_tmp=NULL;
	jm_tmp=(Judge_Msg *)malloc(sizeof(Judge_Msg));

	if(jm_tmp==NULL)
	{
		perror("malloc");
		exit(1);
	}
	
	memcpy(&(jm_tmp->judge_order.pacid),&pacid,sizeof(struct PacId));
	jm_tmp->judge_order.judge=judge;
	
	jm_tmp->msgtype=MSG_TYPE;
	return jm_tmp;
}

//to send the judge to packets_catch module
int sendJudge(Judge_Msg * jdmsg, int msg_queue_id)
{
	int msg_len=sizeof(Judge_Msg)-4;	

	if((msgsnd(msg_queue_id,jdmsg,msg_len,0))==-1)
	{
		perror("msgsnd");
		exit(1);
	}	

	free(jdmsg);

	return 0;
}
//获得一个消息队列，返回该消息队列的id
int getMsgQueue()
{
	int key;
	int queue_id;

	if((key=ftok(MSG_PATHNAME,MSG_PROJID))==-1)
	{
		perror("ftok");
		exit(1);
	}
	
	if((queue_id=msgget(key,IPC_CREAT|0660))==-1)//权限问题？
	{
		perror("msgget");
		exit(1);
	}
	return queue_id;
}



